package crm.clientManagement.managedbean;

import commonInfrastructure.accountManagement.entity.SalesmanEntity;
import commonInfrastructure.accountManagement.session.EmployeeSessionBean;
import javax.ejb.EJB;
import crm.clientManagement.entity.CompanyEntity;
import javax.faces.bean.ManagedBean;
import javax.faces.event.ActionEvent;
import java.util.List;
import crm.clientManagement.session.CompanySessionBean;
import java.io.IOException;
import java.io.Serializable;
import java.text.ParseException;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.servlet.http.HttpServletRequest;

@ManagedBean
@ViewScoped
public class DeleteCompanyManagedBean implements Serializable{
  
   public Long id;  
    @EJB
    CompanySessionBean companySessionBean;
    
    @EJB
    EmployeeSessionBean employee;
    
    private CompanyEntity selectedCompany;
    private boolean editMode;  
    private boolean isCustomer = false;
    
    private boolean canDelete=true;
//    private FacesMessage msg;

    public DeleteCompanyManagedBean() {
        selectedCompany=new CompanyEntity();
        employee = new EmployeeSessionBean();
//        if (msg==null){
//             msg = (FacesMessage) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("msgUpdate");
//             if (msg==null){
//                 System.out.println("@@@@@@@@@@@null");
//             }
//             FacesContext.getCurrentInstance().addMessage(null, msg);
//        }
    }
    
    public void initView(PhaseEvent event){
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        Long loginId = (Long) request.getSession().getAttribute("userId");
        SalesmanEntity user = employee.getSalesmanById(loginId);
        if (user.getSalesManLevel().equals("4")){
            canDelete = true;
        }
        else{
            canDelete = false;
        }
    }
    
    public CompanyEntity getSelectedCompany() {  
        
        return selectedCompany;  
    }  
    public void setSelectedCompany(CompanyEntity selectedCompany) {  
        this.selectedCompany = selectedCompany;  
    } 
    
    public boolean isEditMode() {  
        return editMode;  
    }  
  
    public void setEditMode(boolean editMode) {  
        this.editMode = editMode;  
    }  

    public List<CompanyEntity> getCompanies() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        Long loginId = (Long) request.getSession().getAttribute("userId");
        SalesmanEntity user = employee.getSalesmanById(loginId);
        
        if (user.getSalesManLevel().equals("4")){
            return companySessionBean.getAllCompanies();
        }
        else{
            return companySessionBean.getCompaniesBySalesman(loginId);
        }
        
    }
    
    public boolean viewAddress(CompanyEntity client){
        System.out.println("*******"+client.getCompanyStatus());
        if(client.getCompanyStatus().equals("Customer")){
            return true;
        }
        else{
            return false;
        }
    }

    public void deleteCompany(ActionEvent event)throws ParseException {
       id=(Long)event.getComponent().getAttributes().get("param1");
       companySessionBean.deleteClient(id);
    }
    
    public void editClient(CompanyEntity client) throws IOException{
        if (client.getCompanyStatus().equals("Sales Lead")){
            FacesContext.getCurrentInstance().getExternalContext().getFlash().put("salesLead", client);
            FacesContext.getCurrentInstance().getExternalContext().redirect("editSalesLead.xhtml");
        }
        else {
            FacesContext.getCurrentInstance().getExternalContext().getFlash().put("customer", client);
            FacesContext.getCurrentInstance().getExternalContext().redirect("editCustomer.xhtml");
        }
    }
       
       public void saveChanges(ActionEvent event)throws ParseException
    {
        
        companySessionBean.saveChanges(selectedCompany,"Edit");
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Changes saved.", ""));        
    }

    /**
     * @return the isCustomer
     */
    public boolean isIsCustomer() {
        return isCustomer;
    }

    /**
     * @param isCustomer the isCustomer to set
     */
    public void setIsCustomer(boolean isCustomer) {
        this.isCustomer = isCustomer;
    }

    /**
     * @return the canDelete
     */
    public boolean isCanDelete() {
        return canDelete;
    }

    /**
     * @param canDelete the canDelete to set
     */
    public void setCanDelete(boolean canDelete) {
        this.canDelete = canDelete;
    }
       
    
}
